package cc.mrbird.system.log.service.impl;

import cc.mrbird.system.log.dao.LoginfoMapper;
import cc.mrbird.system.log.model.Loginfo;
import cc.mrbird.system.log.service.LoginfoService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yux
 * @since 2019-07-16
 */
@Service
public class LoginfoServiceImpl extends ServiceImpl<LoginfoMapper, Loginfo> implements LoginfoService {
    @Autowired
    @Lazy
    public LoginfoService loginfoService;
    @Resource
    public LoginfoMapper loginfoMapper;

    @Override
    public IPage<Loginfo> selectMyPage(IPage<Loginfo> page, Wrapper<Loginfo> queryWrapper) {
        System.out.println(queryWrapper.getSqlSelect());
        System.out.println(queryWrapper.getSqlSet());
        System.out.println(queryWrapper.getSqlSegment());
        return loginfoMapper.selectMyPage(page, queryWrapper);
    }

    @Override
    public void trans() {
        Loginfo loginfo = new Loginfo();
        loginfo.setLogId(loginfoMapper.selectOne(new LambdaQueryWrapper<Loginfo>().orderByDesc(Loginfo::getLogId).last("limit 1")).getLogId() + 1);
        loginfoMapper.insert(loginfo);
        System.out.println(3 / 0);
    }

    @Override
    public void transactionalTest() {
        Loginfo loginfo = new Loginfo();
        loginfo.setLogTitle("我到了");
        loginfoMapper.insert(loginfo);
        try {
            loginfoService.transactionalTest2();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //外层事务捕捉异常后也无效 因为标记了已经是回滚状态
    //@Transactional(propagation = Propagation.REQUIRED)
    //嵌套事务，外层事务看是否捕捉了当前事务的异常，如果捕捉了外层事务不会回滚只回滚当前事务，如果未捕捉则外层事务和当前事务都会回滚
    @Override
    public void transactionalTest2() {
        Loginfo loginfo = new Loginfo();
        loginfo.setLogTitle("去哪呢");
        loginfoMapper.insert(loginfo);
        double i = 3 / 0;
    }
}
